package org.example.Soution001;

import java.util.*;

public class BracketIsValid {
    public static void main(String[] args) {
        BracketIsValid biv = new BracketIsValid();
        System.out.println(biv.isValid("(){}}{"));
    }

    public boolean isValid(String s) {
        int len = s.length();
        if(len%2==1) return false;
        Map<Character,Character> map = new HashMap<>();
        map.put(')','(');map.put(']','[');map.put('}','{');
        Deque<Character> deque = new LinkedList<>();
        for(char c:s.toCharArray()) {
            if(map.containsKey(c)){
                if(deque.isEmpty()||deque.peek()!=map.get(c)) return false;
                deque.pop();
            }else{
                deque.push(c);
            }
        }
        return deque.isEmpty();
    }
}
